{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2\n",
    "from tqdm import tqdm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "image = cv2.imread(f'{TRAIN_PATH}/{image_id}.jpg', cv2.IMREAD_COLOR) => BGR \\\\\n",
    "height, width = cv2.imread(imagename).shape[:2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "class AverageMeter():\n",
    "    def __init__(self):\n",
    "        self.reset()\n",
    "    \n",
    "    def reset(self):\n",
    "        self.val = 0\n",
    "        self.avg = 0\n",
    "        self.sum = 0\n",
    "        self.count = 0\n",
    "        \n",
    "    def update(self, val, n=1):\n",
    "        self.val = val\n",
    "        self.sum += val * n\n",
    "        self.count += n\n",
    "        self.avg = self.sum / self.count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "base_path = '.'\n",
    "def get_avgs(country, source, avgs=None):\n",
    "    if avgs is None:\n",
    "        avgs = [AverageMeter(), AverageMeter(), AverageMeter()] # for the three channels\n",
    "    image_path = base_path + f'/{source}/' + country + '/images'\n",
    "    file_list = [filename for filename in os.listdir(image_path) if not filename.startswith('.')]\n",
    "    imgs = []\n",
    "    for filename in  tqdm(file_list):\n",
    "        image = cv2.imread(os.path.join(image_path, filename), cv2.IMREAD_COLOR)\n",
    "        for channel in range(3):\n",
    "            unique_counts = np.unique(image[:, :, channel], return_counts=True)\n",
    "            for i, value in enumerate(unique_counts[0]):\n",
    "                count = unique_counts[1][i]\n",
    "                avgs[channel].update(value, count)\n",
    "    return avgs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 10506/10506 [07:26<00:00, 23.56it/s]\n",
      "100%|██████████| 7706/7706 [06:17<00:00, 20.43it/s]\n",
      "100%|██████████| 2829/2829 [01:54<00:00, 24.63it/s]\n"
     ]
    }
   ],
   "source": [
    "all_train_avgs = get_avgs('Czech', 'train', get_avgs('India', 'train', get_avgs('Japan', 'train', avgs=None)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "122.18988948664389\n",
      "122.63867241806751\n",
      "117.78858424519856\n"
     ]
    }
   ],
   "source": [
    "for avg in all_train_avgs:\n",
    "    print(avg.avg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 10506/10506 [11:09<00:00, 15.69it/s]\n"
     ]
    }
   ],
   "source": [
    "japan_train_avgs = get_avgs('Japan', 'train')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "110.4122186113974\n",
      "112.20143903147343\n",
      "105.63865316444382\n"
     ]
    }
   ],
   "source": [
    "for avg in japan_train_avgs: # BGR\n",
    "    print(avg.avg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 7706/7706 [08:34<00:00, 14.98it/s]\n"
     ]
    }
   ],
   "source": [
    "india_train_avgs = get_avgs('India', 'train')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "133.16387450740845\n",
      "132.82594659134057\n",
      "130.70028839886066\n"
     ]
    }
   ],
   "source": [
    "for avg in india_train_avgs: # BGR\n",
    "    print(avg.avg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 2829/2829 [02:51<00:00, 16.50it/s]\n"
     ]
    }
   ],
   "source": [
    "czech_train_avgs = get_avgs('Czech', 'train')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "124.40346770060091\n",
      "122.78713180157888\n",
      "113.83203764581124\n"
     ]
    }
   ],
   "source": [
    "for avg in czech_train_avgs: # BGR\n",
    "    print(avg.avg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 1313/1313 [01:21<00:00, 16.05it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "110.70592186490865\n",
      "112.58519424758062\n",
      "105.81148197235935\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "japan_test1_avgs = get_avgs('Japan', 'test1')\n",
    "for avg in japan_test1_avgs: # BGR\n",
    "    print(avg.avg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 1314/1314 [01:28<00:00, 14.90it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "111.64751470620682\n",
      "113.39126997681984\n",
      "106.51253742561114\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "japan_test2_avgs = get_avgs('Japan', 'test2')\n",
    "for avg in japan_test2_avgs: # BGR\n",
    "    print(avg.avg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 969/969 [01:06<00:00, 14.48it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "131.86025676368664\n",
      "131.81173556963395\n",
      "129.97003615554408\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "india_test1_avgs = get_avgs('India', 'test1')\n",
    "for avg in india_test1_avgs: # BGR\n",
    "    print(avg.avg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 990/990 [01:04<00:00, 15.29it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "132.13974267559857\n",
      "132.00914381274163\n",
      "130.17438558228895\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "india_test2_avgs = get_avgs('India', 'test2')\n",
    "for avg in india_test2_avgs: # BGR\n",
    "    print(avg.avg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 349/349 [00:21<00:00, 16.38it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "124.54159163482967\n",
      "122.88554165870742\n",
      "114.04832194364852\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "czech_test1_avgs = get_avgs('Czech', 'test1')\n",
    "for avg in czech_test1_avgs: # BGR\n",
    "    print(avg.avg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 360/360 [00:23<00:00, 15.59it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "125.34458052469135\n",
      "123.86319864969136\n",
      "114.83587236882715\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "czech_test2_avgs = get_avgs('Czech', 'test2')\n",
    "for avg in czech_test2_avgs: # BGR\n",
    "    print(avg.avg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
